﻿#include "chinese.h"
#pragma once

#include <QList>
#include <QObject>
#include <QString>

class ShellUtils : public QObject {
    Q_OBJECT
public:
    struct ShellResult {
        /// 是否成功
        bool success = false;

        /// 命令原始结果
        QString cmdResult;

        /// 命令执行返回状态码
        int exitCode;

        /// 命令返回结果
        QString result;

        /// 命令返回结果的说明
        QString msg;

        /// 执行时间，单位毫秒
        long elapsedTime = 0;

        static ShellResult errorResult(QString result, long time) {
            ShellResult shellResult;
            shellResult.success = false;
            shellResult.result = result;
            shellResult.elapsedTime = time;
            return shellResult;
        }
        static ShellResult successResult(QString result, long time) {
            ShellResult shellResult;
            shellResult.success = true;
            shellResult.result = result;
            shellResult.elapsedTime = time;
            return shellResult;
        }
    };

public:
    static ShellUtils::ShellResult runShell(const QString &shellFilePath,
                                            const QStringList &args = {},
                                            const QString &grepPattern = "", int timeout = 0);

    static ShellUtils::ShellResult runCommand(const QString &command, const QStringList &args = {},
                                              const QString &grepPattern = "", int timeout = 0);

    static ShellUtils::ShellResult runCommand(const QStringList &command,
                                              const QString &grepPattern = "", int timeout = 0);

    /// 检查网络是否正常
    /// @param ip 目标IP或域名
    /// @param time PING超时时间，单位秒
    /// @param times PING几次，因为是阻塞式返回结果，所以不能一起PING
    static ShellUtils::ShellResult ping(QString ip, int timeout, int times);

public:
    // 生成匹配类型的拼接字符串
    static QString generateMatchString(QString keyword, QString matchType);
};
Q_DECLARE_METATYPE(ShellUtils::ShellResult)
